package com.brandonflowers.states
{
    import flash.display.LoaderInfo;
    import flash.display.Shape;
    import flash.events.Event;
    import flash.events.IEventDispatcher;
    import flash.events.ProgressEvent;
    import flash.net.URLRequest;
    
    import org.hive.display.LoaderData;
    import org.hive.display.utils.LoaderSet;
    import org.hive.managers.plugin.LayoutManager;
    import org.hive.managers.plugin.LoadManager;
    import org.hive.sequence.ActionSequence;
    import org.hive.sequence.CloseSequence;
    import org.hive.sound.SoundData;
    import org.hive.state.IState;
    import org.hive.state.State;

    public class Main extends State implements IState
    {
        public function create():ActionSequence
        {
            var lm:LayoutManager = LayoutManager.init(hive,hive.canvas);
            var ldm:LoadManager     = LoadManager.init(hive);
            //
            
            ldm.add(new LoaderData(new URLRequest('Panda_6.jpg'),'image3'));
            
            var ls:LoaderSet = new LoaderSet(ldm);
            ls.add( 
            new LoaderData(new URLRequest('donKenn_1.jpg'),'image1'), 
            new LoaderData(new URLRequest('donKenn_2.jpg'),'image2') 
            );
            ls.add('image3');
            
            var snd:SoundData = ls.add( new SoundData(new URLRequest('Alice_Drop_The_Lime.mp3') ));
            snd.addEventListener( Event.COMPLETE, onSoundLoaded );
            
            var img:LoaderData = new LoaderData(new URLRequest('donKenn_3.jpg'),'image4');
            img.contentLoaderInfo.addEventListener( Event.COMPLETE, onImageLoaded );
            ls.add( img );
            
            ls.addEventListener( ProgressEvent.PROGRESS, onImagesProgess );
            ls.addEventListener( Event.COMPLETE, allImagesLoaded );
            
            trace(ldm.loaders.length, ldm.loaders);
            trace(ldm.loaderSets.length, ldm.loaderSets);
            
            var ldrShape:Shape = new Shape();
            ldrShape.graphics.beginFill(Math.random()*0xFFFFFF);
            ldrShape.graphics.drawRect(0,0,lm.stageWidth, lm.stageHeight);
            ldrShape.name = 'preloader';
            //
            var seq:ActionSequence = new ActionSequence('Main');
            seq.push(ldrShape);
            seq.push(ls);
            seq.push( new LoaderData(new URLRequest('sarah_1.jpg')) );
            return seq;
        }
        
        public function close():CloseSequence
        {
            return null;
        }
        
        private function onSoundLoaded( e:Event ):void
        {
            trace(e);
            var snd:SoundData = e.target as SoundData;
            snd.removeEventListener( e.type, onSoundLoaded );
            //snd.play();
        }
        
        private function onImagesProgess( e:ProgressEvent ):void
        {
            trace(e);
            var lm:LayoutManager = hive.getManager(LayoutManager) as LayoutManager;
            var shp:Shape = lm.getChildByName('preloader') as Shape;
            
            shp.scaleX = e.bytesLoaded/100;
        }
        
        private function onImageLoaded( e:Event ):void
        {
            IEventDispatcher(e.target).removeEventListener( e.type, onImageLoaded );
            var lm:LayoutManager = hive.getManager(LayoutManager) as LayoutManager;
            lm.addChild(LoaderInfo(e.target).loader);
            
            trace(LoaderInfo(e.target).loader);
        }
        
        private function allImagesLoaded( e:Event ):void
        {
            IEventDispatcher(e.target).removeEventListener( e.type, allImagesLoaded );
            trace(e);
            
            dispatchEvent(new Event("ALL_LOADED") );
        }
        
    }
}